package com.czxy.controller;


import com.czxy.domain.User;
import com.czxy.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserService userService;




    @GetMapping
    public ResponseEntity findAll(){

        try{

            List<User> list = userService.findAll();

            return  ResponseEntity.ok(list);


        }catch (Exception e){

            e.printStackTrace();
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }



    @PostMapping
    public  ResponseEntity add(User user){

        try{

            userService.add(user);

            return  new ResponseEntity(HttpStatus.NO_CONTENT);
        }catch (Exception e){
            e.printStackTrace();
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }











    }




    @GetMapping("/{uid}")
    public  ResponseEntity findById(@PathVariable("uid") Integer uid){
        try{

            User user = userService.findById(uid);

            return  ResponseEntity.ok(user);


        }catch (Exception e){

            e.printStackTrace();
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }



    }













}
